package com.infindo.appcreate.zzyj.service;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import com.infindo.appcreate.zzyj.util.StringUtil;

public class EmailServiceImpl extends JavaMailSenderImpl implements
		EmailService {
	private static class MailTask implements Runnable {

		private JavaMailSender mailSender;
		private MimeMessage msg;

		public void run() {
			try {
				synchronized (mailSender) {
					mailSender.send(msg);
				}
			} catch (MailException e) {
				e.printStackTrace();
			}
		}

		public MailTask(JavaMailSender mailSender, MimeMessage msg) {
			this.mailSender = mailSender;
			this.msg = msg;
		}
	}

	private static Logger logger = LoggerFactory
			.getLogger(EmailServiceImpl.class);
	private Executor executor;
	public static final String BEAN_NAME = "emailService";
	private Properties javaMailProperties;

	public EmailServiceImpl() {
		javaMailProperties = new Properties();
		executor = Executors.newFixedThreadPool(2);
	}

	public void init() throws Exception {
		updateMailSender();
	}

	public void updateMailSender() {
		setDefaultEncoding("UTF-8");
		setUsername("polygonsmtp");
		setPassword("PS1288#a");
		setHost("smtp.infindo.com");
		javaMailProperties.setProperty("mail.smtp.auth", "true");
		javaMailProperties.setProperty("mail.smtp.starttls.enable", "true");
		setJavaMailProperties(javaMailProperties);
	}

	public void sendTextMail(String receviers[], String replyTo,
			String subject, String content) {
		if (receviers == null || receviers.length < 1)
			return;
		SimpleMailMessage msg = new SimpleMailMessage();
		msg.setFrom("bloggerapp@infindo.com");
		msg.setTo(receviers);
		msg.setSubject(subject);
		msg.setText(content);
		if (StringUtil.isNotBlank(replyTo))
			msg.setReplyTo(replyTo);
		send(msg);
	}

	public void sendMimeMail(String receviers[], String replyTo,
			String subject, String content, File attachments[]) {
		if (receviers == null || receviers.length < 1)
			return;
		MimeMessage msg = createMimeMessage();
		try {
			MimeMessageHelper helper = new MimeMessageHelper(msg, true, "UTF-8");
			helper.setTo(receviers);
			helper.setFrom("noReplyEmail@infindo.com", "BloApp");
			helper.setSubject(subject);
			helper.setText(content, true);
			if (StringUtil.isNotBlank(replyTo))
				helper.setReplyTo(replyTo);
			if (attachments != null && attachments.length > 0) {
				File arr$[] = attachments;
				int len$ = arr$.length;
				for (int i$ = 0; i$ < len$; i$++) {
					File file = arr$[i$];
					helper.addAttachment(file.getName(), file);
				}

			}
		} catch (MessagingException e) {
			e.printStackTrace();
			return;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return;
		}
		try {
			MailTask mailTask = new MailTask(this, msg);
			(new Thread(mailTask)).start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
